package com.zx.screener.web;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zx.screener.service.userCenter.model.Manager;
import com.zx.screener.service.userCenter.model.User;
import com.zx.screener.service.userCenter.service.ManagerService;
import com.zx.screener.service.userCenter.service.UsrService;

@Controller
public class ManagerController {

	@Autowired
	private ManagerService managerService;

	@Autowired
	private UsrService usrService;

	@RequestMapping("/managerLoginPage")
	public String loginPage(ModelMap model, String username, String password,
			HttpServletRequest request) {
		return "managerLogin.jsp";
	}

	/**
	 * 登录
	 */
	@RequestMapping("/managerLogin")
	public String doLogin(ModelMap model, String username, String password,
			HttpServletRequest request) {
		Manager manager = managerService.login(username, password);
		if (null == manager) {
			return "managerLogin";
		}

		request.setAttribute("manager", manager);
		return "managerIndex.jsp";
	}

	@RequestMapping("/createUser")
	public String createUser(ModelMap model, String username) {
		User user = new User();
		user.setUserId(UUID.randomUUID().toString());
		user.setUserName(username);
		try {
			usrService.insertUsr(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "usrlist.form";
	}

	@RequestMapping("/usrlist")
	public String usrlist(ModelMap model) {
		List<User> users = null;
		try {
			users = usrService.queryUsr();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("users", users);
		return "usrlist.jsp";
	}
}
